﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Site.ViewModels;
using SDT.Model;
using SDT.Site.Properties;
using System.Linq.Dynamic;
namespace SDT.Site.Controllers
{
    public class BannerController : Controller
    {
        //
        // GET: /Banner/
        SDTDataContext db = new SDTDataContext();
        public ActionResult Index()
        {
            return View();
        }
        [ChildActionOnly]
        public ActionResult Top(int posid, int menuid, int number, int width, int height, string border, string borderDir)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate <= DateTime.Now && e.ToDate >= DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", border, borderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return View(b);
        }
        [ChildActionOnly]
        public ActionResult LastedLeftTop(int posid, int menuid, int number,int width, int height, string order, string orderDir)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate<=DateTime.Now && e.ToDate >=DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return View(b);
        }
        [ChildActionOnly]
        public ActionResult LastedLeftCenter(int posid, int menuid, int number, int width, int height, string order, string orderDir)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate <= DateTime.Now && e.ToDate >= DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return View(b);
        }
        [ChildActionOnly]
        public ActionResult LastedLeftBottom(int posid, int menuid, int number, int width, int height, string order, string orderDir)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate <= DateTime.Now && e.ToDate >= DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return View(b);
        }
        [ChildActionOnly]
        public ActionResult LastedRightTop(int posid, int menuid, int number, int width, int height, string order, string orderDir)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate <= DateTime.Now && e.ToDate >= DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return View(b);
        }
        [ChildActionOnly]
        public ActionResult LastedRightCenter(int posid, int menuid, int number, int width, int height, string order, string orderDir)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate <= DateTime.Now && e.ToDate >= DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return View(b);
        }
        [ChildActionOnly]
        public ActionResult LastedRightBottom(int posid, int menuid, int number, int width, int height, string order, string orderDir)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate <= DateTime.Now && e.ToDate >= DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return View(b);
        }
        [ChildActionOnly]
        public ActionResult Lasted(int posid, int menuid, int number, int width, int height, string order, string orderDir, string viewName)
        {
            var cUrl = Request.Url.PathAndQuery.ToLower();
            if (cUrl == "/") cUrl = "/home";
            var m = db.Menus.Where(e => e.NavigateUrl.ToLower() == cUrl).FirstOrDefault();
            var mid = (menuid <= 0 && m != null) ? m.ID : menuid;
            BannerViewModels b = new BannerViewModels();
            var l = db.Banners.Where(e => e.BannerMenuMaps.Where(c => c.MenuID == mid).ToList().Count > 0)
                .Where(e => posid > 0 ? e.BannerMenuMaps.Where(c => c.PositionID == posid).ToList().Count > 0 : true)
                .Where(e => e.FromDate <= DateTime.Now && e.ToDate >= DateTime.Now)
                .Where(e => e.PubYN == true).OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            b.Banners = number > 0 ? l.Take(number).ToList() : l.ToList();
            b.MenuID = menuid;
            b.PositionID = posid;
            b.ImageUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerImagePreviewUrlPrefix;
            b.ImagePath = Settings.Default.SiteRootPath + Settings.Default.BannerImageRootPath;

            b.FlashUrl = Settings.Default.SiteRootUrl + Settings.Default.BannerFlashPreviewUrlPrefix;
            b.FlashPath = Settings.Default.SiteRootPath + Settings.Default.BannerFlashRootPath;

            b.Width = width;
            b.Height = height;
            return PartialView(viewName,b);
        }  
    }
}
